package sample.mybatis.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sample.mybatis.dto.StudentDto;
import sample.mybatis.mapper.StudentMapper;

@Service
@Slf4j
public class ServiceB {

    @Autowired
    private StudentMapper studentMapper;

    @Transactional
    public void experience1(){
        StudentDto s1 = new StudentDto();
        s1.setAge(2);
        s1.setName("2");

        try{
            studentMapper.insert(s1);
            int i = 1/0;
        }catch (Exception e){
            log.error("error!");
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void experience2(){
        log.info("====experience2B start!");
        StudentDto s1 = new StudentDto();
        s1.setAge(2);
        s1.setName("2");
        studentMapper.insert(s1);
        log.info("====experience2B end!");
    }

    @Transactional
    public void experience4(){
        StudentDto s1 = new StudentDto();
        s1.setAge(2);
        s1.setName("2");

        studentMapper.insert(s1);
        int i = 1/0;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void experience5(StudentDto s1){
        s1.setAge(10);
        s1.setName("20");
        studentMapper.update(s1);
    }

}
